﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoDbObject;

namespace SZJT.Utility.DataSource
{
    public class Pagination
    {
        private const int DEFAULT_PAGESIZE = 10;
        private const int DEFAULT_PAGEINDEX = 0;
        private int _pageSize = DEFAULT_PAGESIZE;
        private int _pageIndex = DEFAULT_PAGEINDEX;
        IDBBigObjectSet _allRecords;

        #region 构造函数
        public Pagination()
        {
        }

        public Pagination(int pageSize)
        {
            _pageSize = pageSize;
        }

        public Pagination(IDBBigObjectSet allRecords)
        {
            _allRecords = allRecords;
        }

        public Pagination(IDBBigObjectSet allRecords, int pageSize)
        {
            _allRecords = allRecords;
            _pageSize = pageSize;
        }
        #endregion

        #region 属性
        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }

        public int PageIndex
        {
            get { return _pageIndex; }
            set { _pageIndex = value; }
        }
        #endregion

        #region Get Pagination DataSet, update pagination information at same time
        public List<IDBDataObject> GetPageDataSet()
        {
            return GetPageDataSet(_pageIndex, _pageSize);
        }
        
        public List<IDBDataObject> GetPageDataSet(int pageIndex)
        {
            return GetPageDataSet(pageIndex, _pageSize);
        }

        /// <summary>
        /// Get the pagenation records.
        /// </summary>
        public List<IDBDataObject> GetPageDataSet(string queryPageIndex, string queryPageSize)
        {
            int pageSize = 0;
            int pageIndex = 0;

            //if the invalid paging data, then set to be default value.
            if (!int.TryParse(queryPageSize, out pageSize))
            {
                pageSize = _pageSize;
            }

            if (!int.TryParse(queryPageIndex, out pageIndex))
            {
                pageIndex = DEFAULT_PAGEINDEX;
            }

            return GetPageDataSet(pageIndex, pageSize);
        }

        public List<IDBDataObject> GetPageDataSet(int pageIndex, int pageSize)
        {
            List<IDBDataObject> currentPageRecord = new List<IDBDataObject>();

            if (_allRecords.DataCount > 0)
            {
                int currentRow = 0;
                int fromIndex = 0;

                pageSize = (pageSize <= 0 ? DEFAULT_PAGESIZE : pageSize);
                fromIndex = pageIndex * pageSize;

                //if the total count is less than the from index, then get the last page.
                //if (allRecords != null && allRecords.DataCount < fromIndex)
                if(_allRecords.DataCount < fromIndex)
                {
                    int restCount = _allRecords.DataCount % pageSize;

                    if (restCount == 0)
                    {
                        restCount = pageSize;
                    }

                    fromIndex = _allRecords.DataCount - restCount;

                    pageIndex = fromIndex / pageSize;
                }

                _pageIndex = pageIndex;
                _pageSize = pageSize;

                #region Get Fields Data

                _allRecords.MoveTo(fromIndex);
                while (currentRow < pageSize && fromIndex < _allRecords.DataCount)
                {
                    //Get all the field values.
                    CDBDataObject obj = new CDBDataObject(_allRecords.TableName, _allRecords.DBSystem.FieldList, _allRecords.DataSourceLayer);
                    string fieldName = string.Empty;

                    int fieldCount = _allRecords.DBSystem.FieldCount;
                    for (int i = 0; i < fieldCount; i++)
                    {
                        fieldName = _allRecords.DBSystem[i].ColumnName;
                        obj.SetObject(fieldName, _allRecords[fieldName]);
                    }

                    currentPageRecord.Add(obj);

                    _allRecords.MoveNext();
                    currentRow++;
                    fromIndex++;
                }

                #endregion
            }

            return currentPageRecord;
        }
        #endregion

    }
}
